package com.example.guiguiapplicatio.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.guiguiapplicatio.databindingBean.Doctor;
import com.example.guiguiapplicatio.LoginActivity;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentUserBinding;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.example.guiguiapplicatio.utils.PreferencesUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UserFragment extends Fragment {
    private FragmentUserBinding binding;
    private Doctor doctor;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){


        binding=FragmentUserBinding.inflate(inflater,container,false);

        binding.systemSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.action_navigation_user_to_SystemsettingsFragment);
            }
        });
        initUser();
        setLoginOut();
        return binding.getRoot();
    }

    private void setLoginOut() {
        binding.loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog aldg;
                AlertDialog.Builder adBd = new AlertDialog.Builder(getContext());
                adBd.setTitle("提示");
                adBd.setMessage("确定要退出？");
                adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PreferencesUtil.putInt(getContext(), "doctor_id",-1);
                        PreferencesUtil.putString(getContext(),"doctor_gson","0");
                        Intent intent =new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                        if (getActivity()!=null){
                            getActivity().finish();
                        }
                    }

                });
                adBd.setNegativeButton("取消", null);
                aldg = adBd.create();
                aldg.show();

            }
        });
    }

    private void initUser() {

        String doctorGson=PreferencesUtil.getString(getContext(),"doctor_gson");
        if (doctorGson.length()>1){
//            doctorGson=doctorGson.substring(1,doctorGson.length()-1).replaceAll("\\\\","");
            Gson gson=new Gson();
            doctor=gson.fromJson(doctorGson, Doctor.class);
            binding.setDoctor(doctor);
        }

    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.VISIBLE);
        super.onResume();
    }
}
